package refactor.service.impl;

import app.entity.FacMom;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import common.bean.DeviceBean;
import common.standard.table.FacMonitoringFieldEnum;
import common.standard.table.PageEnum;
import common.util.ConditionUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import refactor.entity.FacMonitoring;
import refactor.mapper.FacMonitoringMapper;
import refactor.service.IFacMonitoringService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * VIEW 服务实现类
 * </p>
 *
 * @author 杨健
 * @since 2019-08-19
 */
@Service
public class FacMonitoringServiceImpl extends ServiceImpl<FacMonitoringMapper, FacMonitoring> implements IFacMonitoringService {
    private FacMonitoringMapper facMonitoringMapper;

    @Autowired
    public void setFacMonitoringMapper(FacMonitoringMapper facMonitoringMapper) {
        this.facMonitoringMapper = facMonitoringMapper;
    }

    /**
     * 根据前台参数分页查询该用户所在区域的设备实时信息
     *
     * @param condition 条件
     * @param regionId  区域id
     * @param pageNum 当前页码
     * @return 返回分页对象
     * @exception ParseException 日期转换出错
     */
    @Override
    public IPage<FacMonitoring> pageList(FacMom condition, Integer regionId, Integer pageNum) throws ParseException {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        long startTime = StringUtils.isBlank(condition.getF()) ? 0 : format.parse(condition.getF()).getTime();
        long endTime = StringUtils.isBlank(condition.getT()) ? System.currentTimeMillis() : format.parse(condition.getT()).getTime();

        QueryWrapper<FacMonitoring> facMonitoringQueryWrapper = new QueryWrapper<>();
        facMonitoringQueryWrapper.eq(FacMonitoringFieldEnum.FACREGIONID.getFieldName(), regionId)
                .ge(FacMonitoringFieldEnum.EVENTSTART.getFieldName(), startTime)
                .le(FacMonitoringFieldEnum.EVENTSTART.getFieldName(), endTime);

        // 告警条件
        if (StringUtils.isNotBlank(condition.getYczt())) {
            facMonitoringQueryWrapper.eq(FacMonitoringFieldEnum.EVENTDEVSTU.getFieldName(), condition.getYczt());
        }

        // 设备条件
        DeviceBean deviceBean = new DeviceBean(condition.getJg(), condition.getTdd(), condition.getJfs(), condition.getJgs(),
                condition.getMenjinhezi(),condition.getDianbiao(),condition.getShuibiao(),condition.getYangan());
        ConditionUtil.addRealtimeModId(facMonitoringQueryWrapper, deviceBean);

        // 搜索条件
        if (StringUtils.isNotBlank(condition.getCondition())) {
            facMonitoringQueryWrapper.nested(i -> i.like(FacMonitoringFieldEnum.FACDESC.getFieldName(), condition.getCondition())
                    .or()
                    .like(FacMonitoringFieldEnum.DEVCODE.getFieldName(), condition.getCondition()));
        }

        Page<FacMonitoring> page = new Page<>(pageNum, PageEnum.PAGE_SIZE.getValue());
        return facMonitoringMapper.selectPage(page, facMonitoringQueryWrapper);
    }

    @Override
    public List<Map<String, Object>> getWarningText(int regionId) {
        return facMonitoringMapper.getWarningText(regionId);
    }
}
